﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections.Generic;

namespace Jy123.Web.Manage.User
{
    public partial class Index : MgConfig
    {
        protected int pagesize = 10;
        protected int cpage = 1;
        protected List<int?> count;

        public static string KeyValue = "";
        protected string sort = "user";
        Jy123.Model.User_Main modelUser = new Jy123.Model.User_Main();
        Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            Verify("User/index.aspx");
            string type = Request.QueryString["Type"];
            try
            {
                cpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            catch
            {
            }
            if (type == "del")
            {
                modelUser = bllUser.GetModel(Convert.ToInt32(Request.QueryString["id"]));
                modelUser.State = 1;
                bllUser.Update(modelUser);
                new Jy123.BLL.User.Friend().delFriendByDelUser(Convert.ToInt32(Request.QueryString["id"]));
                ShowUserList();
                //Response.Write("<script language='javascript'>alert('');</script>");
                //MessageBox("删除成功", MsgType.ok);
            }
            else if (type == "Recommended")
            {
                modelUser = bllUser.GetModel(Convert.ToInt32(Request.QueryString["id"]));
                if (int.Parse(Request.QueryString["flag"]) == 1)//推荐
                {
                    modelUser.isRec = 1;
                    bllUser.Update(modelUser);
                    //Response.Write("<script language='javascript'>alert('');</script>");
                    //MessageBox("该会员推荐成功", MsgType.ok);
                }
                else if(int.Parse(Request.QueryString["flag"]) == 2)//取消推荐
                {
                    modelUser.isRec = 0;
                    bllUser.Update(modelUser);
                    //MessageBox("成功取消推荐该会员", MsgType.ok);
                }
                ShowUserList(); 
            }
            else
            {
                if (!IsPostBack)
                {
                    //LoadList();
                    ShowUserList();
                }
            }
        }
        /// <summary>
        /// 转换推荐按钮的数字为中文意思
        /// </summary>
        /// <param name="isRec">被转换前的数值</param>
        /// <returns></returns>
        protected string Rec(object isRec)
        {
            if (Convert.ToInt16(isRec) == 0)
                return "未推荐";
            else
                return "<font color=\"red\">推荐</font>";
            //return "未推荐";
        }

        /// <summary>
        /// 推荐按钮文字及样式替换
        /// </summary>
        /// <param name="rec">是否推荐(绑定的值)</param>
        /// <param name="id">绑定的记录ID</param>
        /// <param name="page">页码</param>
        /// <returns>返回新按钮文字及样式</returns>
        protected string Rec(object rec, object id, object page)
        {
            if (rec != null && Convert.ToInt16(rec) == 0)
            {
                return "<a class=\"btnsty\" href=\"javascript:Recommended(" + id + ",1,'" + page + "');\">推荐</a>";
            }
            else
            {
                return "<a class=\"btnsty cred\" href=\"javascript:Recommended(" + id + ",2,'" + page + "');\">取消推荐</a>";
            }
        }

        /// <summary>
        /// 转换状态按钮的数字为中文意思
        /// </summary>
        /// <param name="status">状态数值代码</param>
        /// <returns></returns>
        protected string ConvertStatus(object status)
        {
            if (Convert.ToInt16(status) == 0)
                return "待验证";
            else if (Convert.ToInt16(status) == 1)
                return "被锁定";
            else if (Convert.ToInt16(status) == 2)
                return "未验证";
            else if (Convert.ToInt16(status) == 5)
                return "正常";
            return "未知状态";
        }
        /// <summary>
        /// 显示会员列表
        /// </summary>
        private void ShowUserList()
        {
            try
            {
                cpage = Convert.ToInt32(Request.QueryString["page"]);
            }
            catch
            {
            }

            int s = 10;
            if (!string.IsNullOrEmpty(Request.QueryString["s"]))
            {
                s = Convert.ToInt32(Request.QueryString["s"]);
            }
            if (!string.IsNullOrEmpty(Key.Text))//搜索框不为空
            {
                //Rpt_UserList.DataSource = bllUser.GetList("email like '%"+Key.Text+"%'");
                Rpt_UserList.DataSource = new BLL.User.UserSql().GetUserList("email like '%" + Key.Text + "%' or UserName like '%"+Key.Text+"%'", cpage, pagesize, out count);
                Rpt_UserList.DataBind();
            }
            else //搜索框为空
            {
                if (s == 10)
                {
                    Rpt_UserList.DataSource = new BLL.User.UserSql().GetUserList("email like '%" + Key.Text + "%'", cpage, pagesize, out count);
                    Rpt_UserList.DataBind();
                }
                else
                {
                    Rpt_UserList.DataSource = new BLL.User.UserSql().GetUserList("state = '" + s + "'",cpage,pagesize,out count);//根据状态检索会员列表
                    Rpt_UserList.DataBind();
                }

            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            ShowUserList();
        }
    }
}
